

App.ErrorController = Ember.Controller.extend({
    error: 'some error here'
});

App.AboutController = Ember.Controller.extend({
    time: function() {
        return (new Date()).toDateString();
    }.property()
});

App.LoginController = Ember.Controller.extend({
    /*
    reset: function() {
        this.setProperties({
            username: "",
            password: "",
            error:    null
        });
    },*/
    actions: {
        login: function() {
            var self = this; // to make available in anon func
            self.set('error', null);
            $.post("server/process.php", {
                username: this.get('username'),
                password: this.get('password'),
                action: 'login'
            }, "json").then(function(data) {
                var cont = $.parseJSON(data);
                self.set('token', cont.token);
                if (typeof cont.error !== 'undefined') {
                    self.set('error', cont.error);
                }
            });
        }
    }
});

App.RegisterController = Ember.Controller.extend({
    /*
    reset: function() {
        this.setProperties({
            username: "",
            password: "",
            error:    null
        });
    },*/
    actions: {
        register: function() {
            var self = this; // to make available in anon func
            var pass = this.get('password');
            var pass2 = this.get('password2');
            var user = this.get('username');
            self.set('error', null);
            if (! validateEmail(user)) {
                self.set('error', "Use a valid email address!");
            }
            else if (pass !== pass2) {
                self.set('error', 'Passwords do not match');
            }
            else if (self.error === null) {
                self.transitionToRoute('index');
            }
            /*
            
            var res = $.post("server/process.php", {
                 username: this.get('username'),
                 password: this.get('password'),
                 action:   'register'
            }, "json");
            res.done(function(data) {
                var cont = $.parseJSON(data);
                self.set('token', cont.token);
                if(typeof cont.error != 'undefined') {
                    self.set('error', cont.error);
                }
            });*/
        }
    }
});

App.EditableController = Ember.ObjectController.extend({
    notEditing: true,
    /* back: function() {
        return this.get('model.name');
    }.property(),*/
    actions: {
        edit: function() {
          //  back = this.get('model.name');
            this.set('notEditing', false);
        },
        save: function() {
            // back = this.get('model.name');
            this.set('notEditing', true);     
            var mod = this.get('model').constructor;
            var obj = {id: this.get('id')};
            this.store.packAndCreateUpdate(obj, mod, this, false);
        },
        cancel: function() {
            //this.set('model.name', back);
            this.set('notEditing', true);
        },
        undo: function() {
           // this.set('model.name', back);
            this.set('notEditing', true);
        }
    }
});

App.FieldController = App.EditableController.extend({
});

App.UserController = App.EditableController.extend({
});

App.TeamController = App.EditableController.extend({
});

App.UserinfoController = App.EditableController.extend({
});

App.CreateuserController = Ember.ArrayController.extend({
    actions: {
        create: function() {
            var mod = App.User;
            var obj = {permission: 'User'};
            this.store.packAndCreateUpdate(obj, mod, this, true);
        }
   }
});

App.CreateteamController = Ember.ArrayController.extend({
    actions: {
        create: function() {
            var mod = App.Team;
            var obj = {};
            this.store.packAndCreateUpdate(obj, mod, this, true);
        }
   }
});

App.CreatefieldController = Ember.ArrayController.extend({
    actions: {
        create: function() {
            //var fields = ['name', 'age', 'pclass', 'location'];
            var mod = App.Field;
            var obj = {};
            this.store.packAndCreateUpdate(obj, mod, this, true);
        }
   }
});

